<?php
require_once 'Log.php';

class DatabaseConnection
{
	private $m_databaseName = "";
	private $m_mySqliObject = NULL;
	private $m_dbHost = NULL;
	private $m_dbUsername = NULL;
	private $m_dbPassword = NULL;
	private $m_tablePrefix = NULL;
	
	public function __construct($dbHost, $dbUsername, $dbPassword, $database, $tablePrefix)
    {
		$this->m_dbHost = $dbHost;
		$this->m_dbUsername = $dbUsername;
		$this->m_dbPassword = $dbPassword;
		$this->m_databaseName = $database;
		$this->m_tablePrefix = $tablePrefix;
	}
	
	// Hämtar ut tabellprefix.
	public function getTablePrefix()
	{
		return $this->m_tablePrefix;
	}
	
	// Hämtar databasensnamn
	public function getDatabaseName()
	{
		return $this->m_databaseName;
	}

	public function Connect()
        {
		$this->m_mySqliObject = new mysqli($this->m_dbHost, $this->m_dbUsername, $this->m_dbPassword, $this->m_databaseName);
 
		$this->m_mySqliObject->set_charset("utf8");
		
		if (mysqli_connect_errno())
        {
		    // printf("Connect failed: %s\n", mysqli_connect_error());
			$Log = new Log();
			
			$Log->LogError("Fel vid anslutning till databasen.");
			exit();
			return false;
		}
		
		return true;
		
	}

	public function Query($query)
	{
		return $this->m_mySqliObject->query($query);	
	}
	
	public function Close()
    {
		$this->m_mySqliObject->close();
	}

	public function PrepareStatement($sqlStatement)
    {
		return $this->m_mySqliObject->prepare($sqlStatement);
	}
}